/**
 * Created by sens on 2020/5/2.
 */
(function () {
    typeof window.sens !== 'object' && (window.sens = {});
})(window);

Object.assign(window.sens, (function () {
    let maxProp = 1000;
    let maxNameProp = 100;

    function Player(name) {
        let name_md5 = hex_md5(name);
        //职业
        let job = ['疯狂的', '玩火的', '智障的', '仁慈的', '傻子', '屠夫', '碰瓷者', '光棍', '傲娇', '屠龙者', '妻管严', '女汉子', '娘娘腔', '神经病', '老司机', '缺心眼'];

        /**
         * 生成属性数值
         * @param arr
         * @returns {number}
         */
        function joint(arr) {
            let sum = '0';
            for (i in arr) {
                sum += name_md5.indexOf(arr[i]) == -1 ? '' : name_md5.indexOf(arr[i]).toString();
            }
            return parseInt(sum);
        }

        // 4 job
        let jobNum = Math.floor(parseInt(name_md5.substr(0, 1), 16) / 4);

        let fixProp = name === '小' ? 25 : 50;

        this.HP = joint(['1', '2', '3']) % (maxNameProp * 10) + 10 * fixProp;

        this.att = joint(['4', '5']) % maxNameProp + fixProp;

        this.def = joint(['6', '7']) % maxNameProp + fixProp;

        this.int = joint(['8', '9']) % maxNameProp + fixProp;

        this.skill = joint(['0', 'a']) % maxNameProp + fixProp;

        this.agl = joint(['b', 'c']) % maxNameProp + fixProp;

        this.spe = joint(['d', 'e']) % maxNameProp + fixProp;

        this.oriName = name;

        let map = {
            HP: '壮士',
            att: '杀手',
            def: '骑士',
            int: '智者',
            skill: '小丑',
            agl: '忍者',
            spe: '刺客'
        };

        let maxNum = 0;
        this.HP = (this.HP - 9 * fixProp) / 10;

        for (key in map) {
            if (this[key] > maxNum) {
                this.duty = map[key];
                maxNum = this[key];
            }
        }

        this.HP = this.HP * 10 + 9 * fixProp;

        this.job = job[jobNum];
        this.jobNum = jobNum;
        this.name = job[jobNum] + this.duty + '-' + name;

        this.skill -= 40;
        this.skills = sens.jopMap[this.jobNum].skills;

        // 适应性，越高加成越多
        this.z = joint(['f']) % 100;

        this.debuff = {
            name: '无',
            num: 0
        };
    }

    function makePlayer (name, words) {
        let nameArr = name.split('');
        let playerArr = nameArr.map(function (sname) {
            return new Player(sname)
        });
        let player = mergePlayer(playerArr);
        player.words = words || name;

        return player;
    }

    function mergePlayer (players) {
        let map = {
            HP: '壮士',
            att: '杀手',
            def: '骑士',
            int: '智者',
            skill: '小丑',
            agl: '忍者',
            spe: '刺客'
        };
        let fixProp = 50;
        let sPlayer = players[0];
        let percent = Math.pow(0.8 * (players.length - 1)) || 1;
        let props = ['HP', 'att', 'def', 'int', 'skill', 'agl', 'spe'];

        props.forEach(function (prop) {
            sPlayer[prop] = Math.round(sPlayer[prop] * percent);
        });

        players.shift();

        players.length && players.forEach(function (player, idx) {
            let addPer = 0;
            let z = player.z;

            while (z > 5) {
                addPer += 0.02;
                z -= 5;
            }

            if (player.duty === sPlayer.duty) {
                addPer += 0.1;
            }

            props.forEach(function (prop) {
                sPlayer[prop] += Math.round(player[prop] * (0.2 + addPer));
            });

            player.skills.forEach(function (skill) {
                skill.rate = Math.round(skill.rate / 2);
                sPlayer.skills.push(skill)
            });

            sPlayer.oriName += player.oriName;
            sPlayer.name += player.oriName;
        });

        let maxNum = 0;
        sPlayer.HP = (sPlayer.HP - 9 * fixProp) / 10;

        for (key in map) {
            if (sPlayer[key] > maxNum) {
                sPlayer.duty = map[key];
                maxNum = sPlayer[key];
            }
        }

        sPlayer.HP = sPlayer.HP * 10 + 9 * fixProp;
        sPlayer.name = sPlayer.job + sPlayer.duty + '<' + sPlayer.oriName + '>';

        return sPlayer;
    }

    return {
        Player: Player,
        makePlayer: makePlayer,
        mergePlayer: mergePlayer
    }
})());
